// pages/present/index.js
const app = getApp()
import Toast from "../../vant/weapp/toast/toast";
Page({
  data: {
    cardlist: [],
    roomOptions: [],
    diy: 2,
    style: null,
    store:0,
    isCardlist: true
  },
  isInArray: function(room_id, roomids) {
    return Array.isArray(roomids) && roomids.includes(room_id);
  },
  onLoad(options) {
    const that = this;
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    }else{
      this.setData({
        store_id: options.store_id
      }); 
    }
    that.getPresent(options.store_id)
  },
  getPresent(store) {
    const that = this
    app.http.post(app.api.coupon, {
      store_id: that.data.store_id
    }, (res) => {
      if (res.data.lists == "") {
        that.setData({
          isCardlist: true
        })
      } else {
        that.setData({
          cardlist: res.data.lists,
          roomOptions: res.data.roomOptions,
          isCardlist: false
        })
      }
    })
  },
  //   领取
  receive(e) {
    const that = this
    let coupon_id = e.currentTarget.dataset.id
    app.http.post(app.api.receive, {
      coupon_id
    }, (res) => {
      if (res.code == 200) {
        setTimeout(() => {
          Toast({
            type: "success",
            message: '领取成功',
            forbidClick: true,
            duration: 1000,
            onClose: () => {
              that.getPresent()
            },
          });
        }, 0)

      }
    })
  }
});